package com.mybatisplus.demo.base;

import java.io.IOException;
import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import java.util.Optional;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @CLASSNAME CreateStreams
 * @DESCRIPTION 描述
 * @AUTHOR chen
 * @DATE 2019-10-25 22:43
 **/
public class CreateStreams {

    public static void main(String[] args) throws IOException {
//        String content = new String( Files.readAllBytes( Paths.get( "e://profile/word.txt" ) ), StandardCharsets.UTF_8 );
//        List <String> words = Arrays.asList( content.split( "\\PL+" ) );
        //"\\PL+"表示为非字母分隔符,类似的还有"\\PN+" 非数字分隔符 "\\PZ+" 非分隔符 "\\PS+" 非符号
//        System.out.println( "单词个数:" + words.size() );

        Path path = Paths.get( "d://profile/word.txt" );
        String content = new String( Files.readAllBytes( path ), StandardCharsets.UTF_8 );
        Stream <String> words = Stream.of( content.split( "\\PL+" ) );
        show( "word", words );

        Stream <String> song = Stream.of( "gently", "down", "the", "stream" );
        show( "song", song );
        Stream <String> since = Stream.empty();
        show( "since", since );

        Stream <String> echo = Stream.generate( () -> "echo" );
        show( "echo", echo );

        Stream <Double> random = Stream.generate( Math::random );
        show( "random", random );

        Stream <BigInteger> integer = Stream.iterate( BigInteger.ONE, n -> n.add( BigInteger.ONE ) );
        show( "integer", integer );

        Stream <String> wordAnotherWay = Pattern.compile( "\\PL+" ).splitAsStream( content );
        show( "wordAnotherWay", wordAnotherWay );

        try (Stream <String> lines = Files.lines( path, StandardCharsets.UTF_8 )) {

            show( "lines", lines );
        }

        List <String> word = Arrays.asList( content.split( "\\PL+" ) );
        long cont = word.parallelStream().filter( w ->w.equals( "a" ) ).count();
        System.out.println(cont);

        Stream<String> lower =word.stream().map( String::toLowerCase );
        System.out.println(lower);

        Stream<String> uniqueWords= Stream.of( "a","b","a","v","c" ).distinct();
        uniqueWords.forEach(System.out::println );

        Optional<String> largest = words.max( String::compareToIgnoreCase );
        System.out.println(largest.orElse( "" ));

        String result = largest.orElseGet(()-> Locale.getDefault().getDisplayName() );
        System.out.println(result);
        System.out.println(inverse( .00 ));


    }

    private static <T> void show(String title, Stream <T> stream) {
        final int SIZE = 10;
        List <T> firstElements = stream.limit( SIZE + 1 ).collect( Collectors.toList() );
        System.out.println( title + ":" );
        for (int i = 0; i < firstElements.size(); i++) {
            if (i > 0) System.out.print( "," );
            if (i < SIZE) System.out.print( firstElements.get( i ) );
            else System.out.print( "..." );
        }
        System.out.println();
    }

    private static Optional<Double> inverse (Double x){
        return x == 0?Optional.empty():Optional.of( 1 / x );
    }
}
